package com.chenbei.erp.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenbei.common.domain.vo.UploadFileResult;
import com.chenbei.common.enums.FileSizeUnitEnum;
import com.chenbei.common.exception.LogicHandleException;
import com.chenbei.common.service.IFileUploadService;
import com.chenbei.common.util.FileUtil;
import com.chenbei.erp.domain.ProductBrand;
import com.chenbei.erp.mapper.ProductBrandMapper;
import com.chenbei.erp.service.IProductBrandService;
import com.chenbei.erp.vo.req.ProductBrandReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@Service
public class ProductBrandService extends ServiceImpl<ProductBrandMapper, ProductBrand> implements IProductBrandService {

    @Autowired
    private IFileUploadService fileUploadService;

    @Override
    public void add(ProductBrand productBrand) {
        LambdaQueryWrapper<ProductBrand> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProductBrand::getName, productBrand.getName());
        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicHandleException("名称已存在");
        }
        save(productBrand);
    }

    @Override
    public void update(ProductBrand productBrand) {
        LambdaQueryWrapper<ProductBrand> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ne(ProductBrand::getId, productBrand.getId())
                .eq(ProductBrand::getName, productBrand.getName());
        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicHandleException("名称已存在");
        }
        updateById(productBrand);
    }

    @Override
    public Page<ProductBrand> pageList(ProductBrandReq productBrandReq) {
        Page<ProductBrand> pageConfig = new Page<>(productBrandReq.getPageNum(), productBrandReq.getPageSize());
        LambdaQueryWrapper<ProductBrand> lambdaQueryWrapper = buildWrapper(productBrandReq);
        return page(pageConfig, lambdaQueryWrapper);
    }

    @Override
    public List<ProductBrand> list(ProductBrandReq productBrandReq) {
        LambdaQueryWrapper<ProductBrand> lambdaQueryWrapper = buildWrapper(productBrandReq);
        return list(lambdaQueryWrapper);
    }

    @Override
    public UploadFileResult uploadFile(MultipartFile file) {
        if (!FileUtil.isImage(file)) {
            throw new LogicHandleException("只允许上传图片类型的文件");
        }
        if (!FileUtil.checkFileSize(file, 5, FileSizeUnitEnum.MB)) {
            throw new LogicHandleException("只允许上传不超过5M的文件大小");
        }
        return fileUploadService.upload(file);
    }

    private LambdaQueryWrapper<ProductBrand> buildWrapper(ProductBrandReq productBrandReq) {
        LambdaQueryWrapper<ProductBrand> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(productBrandReq.getName())) {
            lambdaQueryWrapper.like(ProductBrand::getName, productBrandReq.getName());
        }
        if (productBrandReq.getStatus() != null && productBrandReq.getStatus() >= 0) {
            lambdaQueryWrapper.eq(ProductBrand::getStatus, productBrandReq.getStatus());
        }
        return lambdaQueryWrapper;
    }
}
